package cn.net.greenet.traffic.flow.group;

import cn.net.greenet.common.db_service.BaseDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 编辑流量流向策略区域与区域组
 *
 * @author liangdelin
 */
@WebServlet(name = "Edit_Traffic", urlPatterns = {"/traffic_flow/edit_areas"})
public class Edit_Traffic extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String json = request.getParameter("json");
        try {
            JSONObject jo = JSONObject.fromObject(json);
            int old_group_id = Integer.parseInt(jo.getString("old_group_id"));
            String new_group_name = jo.getString("new_group_name");
            JSONArray jor = jo.getJSONArray("areas");  //区域组名称
            String areas = "";
            for (int i = 0; i < jor.size(); i++) {
                areas += jor.getString(i) + ",";
            }
            BaseDao baseDao = new BaseDao();
            int row = baseDao.executeUpdate("update traffic_flow_group_area set `group_name` = ? ,`areas` = ? where id = ?", new Object[]{new_group_name, areas.substring(0, areas.length() - 1), old_group_id});
            if (row != 0) {
                out.println("{\"result\":\"true\"}");
            } else {
                out.println("{\"result\":\"false\"}");
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
